package service.Impl;

import Utils.ReflectUtils;
import Utils.db.pojo.Page;
import dao.UserDAO;
import dao.impl.UserDAOImpl;
import pojo.User;
import service.UserService;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author d11yu
 */
public class UserServiceImpl implements UserService {
    UserDAO userDAO = new UserDAOImpl();

    @Override
    public User register(User user) throws SQLException {
        return userDAO.addUser(user);
    }

    @Override
    public User login(User user) throws SQLException {
        return userDAO.queryUserByUsernameAndPassword(user.getUsername(), user.getPassword());
    }

    @Override
    public boolean exists(User user) throws SQLException {
        if (userDAO.queryUserByUsername(user.getUsername()) == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean exists(String username) throws SQLException {
        if (userDAO.queryUserByUsername(username) == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public Page<User> getPage(Integer pn, Integer ecp, String urlAction) throws SQLException {
        return Page.getPage(pn, ecp, urlAction, userDAO);
    }

    @Override
    public Page<User> getPageByUsernameBlur(Integer pn, Integer ecp, String urlAction, String username) throws SQLException, InvocationTargetException, IllegalAccessException {
        return Page.getPage(pn, ecp, urlAction, userDAO, ReflectUtils.getMethod(userDAO, "getEctByUsernameBlur", String.class), ReflectUtils.getMethod(userDAO, "queryBeanListPerPageByUsernameBlur", Integer.class, Integer.class, String.class), username);
    }

    @Override
    public void update(User user) {
        userDAO.updateUserById(user);
    }

    @Override
    public void deleteById(Integer id) {
        userDAO.deleteUserById(id);
    }

    @Override
    public User queryById(Integer id) throws SQLException {
        return userDAO.queryById(id);
    }
}
